<?php

interface DatabaseInterface{

	public function connect();
	public function disconnect();
	public function isConnected();
	public function query($query);
	public function fetchArray($queryId=-1);
	
}

class MYSQLDatabase implements DatabaseInterface{
	
	private $_server, $_user, $_pasw, $_db, $_connection;
	private $_queryId;
	
	/**
	 * Constructor
	 * @param string $server
	 * @param string $user
	 * @param string $pasw
	 * @param string $db
	 * @return void
	 * @access private
	 */
	function __construct($server, $user, $pasw, $db){
		$this->_MYSQLDatabase($server, $user, $pasw, $db);
	}
	
	/**
	 * Constructor
	 * @param string $server
	 * @param string $user
	 * @param string $pasw
	 * @param string $db
	 * @return void
	 * @access private
	 */
	private function _MYSQLDatabase($server, $user, $pasw, $db){
		$this->_server = $server;
		$this->_user = $user;
		$this->_pasw = $pasw;
		$this->_db = $db;
	}
	
	/**
	 * @access private
	 * @return void
	 */
	function __destruct(){
		if($this->isConnected())
			$this->disconnect();
	}
	
	/**
	 * Connects to specified database.
	 * 
	 * @return void
	 */
	public function connect(){
		// Connecting, selecting database
		$link = @mysql_connect($this->_server, $this->_user, $this->_pasw);
		if($link){
			$suc = @mysql_select_db($this->_db, $link);
			if(!$suc){
				throw new Exception('Could not select database: ' + mysql_error($link));
			}
			else {
				$this->_connection = $link;
			}
		}else{
			throw new Exception('Could not connect: ' + mysql_error());
		}
	}
	
	/**
	 * Query a sql-message
	 * 
	 * @param	string
	 * @return	resource
	 * 
	 */
	public function query($sql){
		if($this->isConnected()){
			$query_id = mysql_query($sql, $this->_connection);
			if(!$query_id){
				echo $sql;
				throw new Exception('Could not query. '. mysql_errno($this->_connection) .' : '. mysql_error($this->_connection));
			}
			else{
				$this->_queryId = $query_id;
				return $query_id;
			}
		}
	}
	
	/**
	 * Returns array which is the result of the query executed prev or given in the param.
	 * @param resource
	 * 		  |resource pointing to a executed query.
	 * @return array of first row.
	 */
	 public	function fetchArray($queryId=-1){
	    // retrieve row
	    if ($query_id!=-1) {
	        $this->_queryId=$query_id;
	    }
	
	    if (isset($this->_queryId)) {
	        return mysql_fetch_assoc($this->_queryId);
	    }else{
	        throw new Exception("Invalid query_id: <b>$this->_queryId</b>. Records could not be fetched.");
	    }
	
	    return $record;
	}
	
	/**
	 * Disconnects from given db.
	 * 
	 * @return void
	 */
	public function disconnect(){
		mysql_close($this->_connection);
		unset($this->_connection);
	}
	
	/**
	 * Returns true if a connection to the given db is up.
	 * 
	 * @returns boolean
	 */
	public function isConnected(){
		return isset($this->_connection);
	}
	
	
	/**
	 * 
	 * @param 	string
	 * @return 	string
	 */
	public static function escape($string) {
	    if(get_magic_quotes_runtime()) 
	    	$string = stripslashes($string);
	    return mysql_real_escape_string($string);
	}
}
?>